package com.cskaoyan.controller;

import com.cskaoyan.bean.promotion.Coupon;
import com.cskaoyan.service.promotion.CouponService;
import com.cskaoyan.utils.Converter;
import com.cskaoyan.utils.PojoUtils;
import com.cskaoyan.vo.*;
import com.cskaoyan.vo.promotion.CouponListVo;
import com.cskaoyan.vo.promotion.CouponListuserVo;
import com.cskaoyan.vo.promotion.CouponReadVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @autyor:caicaicai
 */
@RestController
public class CouponController {

    @Autowired
    CouponService couponService;

    //page=1&limit=20&name=%E7%94%B5%E8%84%91&type=0&status=0&sort=add_time&order=desc
    @RequestMapping("/admin/coupon/list")//page=1&limit=20&sort=add_time&order=desc
    @RequiresPermissions("admin:coupon:list")
    public BaseRespVo list(Integer page, Integer limit,String name, Short type, Short status,
                           String sort, String order){
        CouponListVo couponLsitVo = couponService.query(page, limit, sort, order, name, type, status);
        return BaseRespVo.ok(couponLsitVo);
    }

    @RequestMapping("/admin/coupon/create")
    @RequiresPermissions("admin:coupon:create")
    public BaseRespVo create(@RequestBody Coupon coupon){
        Coupon couponVo = couponService.create(coupon);
        return BaseRespVo.ok(couponVo);
    }

    //请求参数多了daystype，数据库中没有，对修改数据库表没有影响，可以直接丢弃么？
/*    @RequestMapping("/admin/coupon/update")
    public BaseRespVo update(@RequestBody CouponUpdateBo couponUpdateBo){
        int code = couponService.update(couponUpdateBo);
        if(code != 200  ){
            return BaseRespVo.fail("编辑失败！");
        }
        return BaseRespVo.ok(couponUpdateBo);
    }*/
    @RequestMapping("/admin/coupon/update")
    @RequiresPermissions("admin:coupon:update")
    public BaseRespVo update(@RequestBody Coupon coupon) {
        int code = couponService.update(coupon);
        if (code != 200) {
            //return BaseRespVo.fail("编辑失败！");
            return BaseRespVo.error(402,"编辑失败！");
        }
        return BaseRespVo.ok(coupon);
    }

    @RequestMapping("/admin/coupon/delete")
    @RequiresPermissions("admin:coupon:delete")
    public BaseRespVo delete(@RequestBody Coupon coupon){
        int code = couponService.delete(coupon);
        if (code != 200) {
            //return BaseRespVo.fail("编辑失败！");
            return BaseRespVo.error(402,"编辑失败！");
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("/admin/coupon/read")
    @RequiresPermissions("admin:coupon:read")
    public BaseRespVo read(Integer id){
        Coupon coupon = couponService.read(id);
        CouponReadVo couponReadVo = new CouponReadVo();
        Converter converter = PojoUtils.converter();
        converter.converter(coupon, couponReadVo);
        return BaseRespVo.ok(couponReadVo);
    }

    //?page=1&limit=20&couponId=38&userId=1&status=2&sort=add_time&order=desc
    @RequestMapping("admin/coupon/listuser")
    @RequiresPermissions("admin:coupon:listuser")
    public BaseRespVo listuser(Integer page, Integer limit, Integer couponId, String sort, String order, Integer userId, Short status){
        CouponListuserVo couponListuserVo = couponService.listuser(page, limit, couponId, sort, order, userId, status);
        return BaseRespVo.ok(couponListuserVo);
    }

}
